

*-------------------------------------------------------------------------------
* population data
*-------------------------------------------------------------------------------


* population: city of Chicago
clear 
set obs 120
gen month = tm(2010m6) if _n == 1
replace month = month[_n-1] + 1 if month[_n-1] ~= .
format month %tm
gen pop = 2695598 if _n == 1
replace pop = 2746388 if month == tm(2020m5)

	/*
	Population, Census, April 1, 2020	2,746,388
	Population, Census, April 1, 2010	2,695,598
	*/
	
gen ln_pob = ln(pop)
ipolate ln_pob month, gen(ln_pob_i)
gen pop_cgr = exp(ln_pob_i)
tempfile pop_chicago
savesome month pop_cgr using `pop_chicago'


* population: Cook County
import delimited "data/inputs/other/il cities population.csv", clear 
keep if name == "Cook County"
assert _N == 1
keep name popestimate*

reshape long popestimate, i(name) j(year, string)
gen month = tm(2020m4) if year == "042020"
replace year = "01/" + year if length(year) == 4
replace month = monthly(year, "MY") if month == .
format month %tm

egen countyid = group(name)
tsset countyid month
tsfill, full

gen ln_pob = ln(popest)
by countyid: ipolate ln_pob month, gen(ln_pob_i)
gen pop_cookcounty = exp(ln_pob_i) 

keep month pop_cookcounty

tempfile pop_cookcounty
savesome month pop using `pop_cookcounty'
	
	

*-------------------------------------------------------------------------------
* contact card data, ISR data, traffic stop data
*-------------------------------------------------------------------------------


* contact card data
use "data/outputs/CC_beat.dta", clear
collapse (sum) pedestrian, by(month)
tempfile cc
save `cc'


* traffic stop data
use "data/outputs/TSSS_beat.dta", clear

replace agencyname = "troopers" if regexm(agencyname, "ILLINOIS")
replace agencyname = "cpd" if regexm(agencyname, "CHICAGO")
replace agencyname = "suburbs" if agencyname ~= "troopers" & agencyname ~= "cpd"

collapse (sum) count, by(agencyname month)

reshape wide count, i(month) j(agencyname, string)
renvars count*, prefix(itss)

tempfile itss
save `itss'


* isr data
use "data/outputs/ISRpedestrian_beat.dta", clear
collapse (sum) isr_count_pedestrian, by(month)
tempfile isr
save `isr'
	
	

*-------------------------------------------------------------------------------
* merge
*-------------------------------------------------------------------------------

* merge
use `itss', clear
merge 1:1 month using `cc'
drop _m
merge 1:1 month using `isr'
drop _m
merge 1:1 month using `pop_chicago'
drop _m
merge 1:1 month using `pop_cookcounty'
	
	

*-------------------------------------------------------------------------------
* calculate rates
*-------------------------------------------------------------------------------
	
* annualized (x12) rates per 100 people
gen pedrate_cpd = 100 * 12 * pedestrian / pop_cgr
gen trafficrate_cpd = 100 * 12 * itsscountcpd / pop_cgr
gen trafficrate_troopers = 100 * 12 * itsscounttroopers / pop_cookcounty
	
* log rates
gen ln_pedrate = ln(pedrate)
gen ln_trafficrate_cpd = ln(trafficrate_cpd)
gen ln_trafficrate_troopers = ln(trafficrate_troopers)
			
* shaded region
gen treatment = 65000 if (month >= tm(2015m8) & month <= tm(2016m1))
gen treatment_rates = 29.2 if (month >= tm(2015m8) & month <= tm(2016m1))
gen treatment_lnrates = 3.38 if (month >= tm(2015m8) & month <= tm(2016m1))
gen treatment_lnrates_negative = -1 if (month >= tm(2015m8) & month <= tm(2016m1))
gen treatment_trooperrates = 1.5 if (month >= tm(2015m8) & month <= tm(2016m1))

	


			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
	


*-------------------------------------------------------------------------------
* graph
*-------------------------------------------------------------------------------

* troopers only
#delimit;
twoway (area treatment_trooperrates month if month >= tm(2012m1) & month <= tm(2020m2), lcolor(gs15%100) fcolor(gs15%100))
	(connected trafficrate_troopers month  if month >= tm(2012m1) & month <= tm(2020m2),
		lcolor(gs8%60) mcolor(gs8%60) msymbol(triangle) msize(vsmall)),
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
ytitle("Stops per 100 per Year", size(medlarge))
yscale(lcolor(none))
ylabel(0(0.25)1.5, labsize(medlarge) glcolor(white) angle(horizontal))
xtitle("", size(medsmall)) 
xscale(lcolor(none))
xlabel(624(12)723, labsize(medlarge) format(%tmCY)) 
legend(off);
graph export "figures/FigureS2a.pdf", replace;


* log rates
#delimit;
twoway (area treatment_lnrates month if month >= tm(2012m1) & month <= tm(2020m2), lcolor(gs15%100) fcolor(gs15%100))
	(area treatment_lnrates_negative month if month >= tm(2012m1) & month <= tm(2020m2), lcolor(gs15%100) fcolor(gs15%100))
	(connected ln_trafficrate_troopers month  if month >= tm(2012m1) & month <= tm(2020m2),
		lcolor(gs8%60) mcolor(gs8%60) msymbol(triangle) msize(vsmall))
	(connected ln_pedrate month if month >= tm(2012m1) & month <= tm(2020m2), 
		mcolor(black) msize(vsmall) lcolor(black) msymbol(O))
	(connected ln_trafficrate_cpd month if month >= tm(2012m1) & month <= tm(2020m2), 
		lcolor(blue%80) lwidth(medthick) msymbol(square) mcolor(blue%80) msize(vsmall)), 
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
ytitle("Stops per 100 per Year (log scale)", size(medlarge))
yscale(lcolor(none))
ylabel(0 "1" 
       `=ln(5)' "5"
	   `=ln(10)' "10" 
	   `=ln(15)' "15"
	   `=ln(20)' "20"
	   `=ln(25)' "25", labsize(medlarge) glcolor(white) angle(horizontal))
xtitle("", size(medsmall)) 
xscale(lcolor(none))
xlabel(624(12)723, labsize(medlarge) format(%tmCY)) 
legend(order(4 "CPD Pedestrian Stops" 5 "CPD Traffic Stops" 3 "ISP Traffic Stops")
symxsize(*.25) size(small) rows(1));
graph export "figures/FigureS2b.pdf", replace;

